package com.metaverse.backend.security;

import com.metaverse.backend.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    public static JwtUser create(User user) {
        JwtUser jwtUser =  new JwtUser(user, mapToGrantedAuthorities(user.getAuthorities()));
        return jwtUser;
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(Set<Authority> authorities) {
        if (authorities != null) {
            return authorities.stream()
                              .map(authority -> new SimpleGrantedAuthority(authority.getName()))
                              .collect(Collectors.toList());
        }
        return null;
    }
}
